package com.example.oms.service.impl;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.example.oms.service.SendCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class SendCodeServiceImpl implements SendCodeService {
    //短信参数：
    private String regionId = "cn-hangzhou";
    private String accessKeyId = "LTAI5tQBp6MLSZ6Pek5NWH78";
    private String secret = "CO33LtYqakcDzzrwHUg45TIVqGqms4";
    private String signName = "阿里云短信测试";//我们在阿里云设置的签名
    private String templateCode = "SMS_154950909";//我们在阿里云设置的模板

    @Override
    public boolean sendCodeByPhone(String userPhone,String code) {
        //调用阿里云提供的短信服务
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, secret);

        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        //设置地区
        request.setSysRegionId(regionId);
	    //设置电话号码
        request.setPhoneNumbers(userPhone);
        //设置签名
        request.setSignName(signName);
	    //设置模板
        request.setTemplateCode(templateCode);
	    //在设置模板的时候有一个占位符
        request.setTemplateParam("{\"code\":\""+code+"\"}");

        try {
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println("短信发送成功");
            return true;
        }catch (ClientException e) {
            e.printStackTrace();
            return false;
        }
    }

}
